import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';

// 自定义 ScrollBehavior，定制滚动条样式
class SukietListViewScrollBehavior extends ScrollBehavior {
  @override
  Widget buildScrollbar(BuildContext context, Widget child, ScrollableDetails details) {
    return GestureDetector(
      child: Padding(
        padding: const EdgeInsets.fromLTRB(0, 0, 0, 0),
        child: ScrollbarTheme(
          data: ScrollbarThemeData(
            thumbColor: WidgetStateProperty.resolveWith((Set<WidgetState> states) {
              if (states.contains(WidgetState.hovered) || states.contains(WidgetState.dragged)) {
                return Colors.white; // Hover 时白色
              }
              return Colors.white60; // 平常灰白色
            }),
          ),
          child: Scrollbar(
            controller: details.controller,
            thumbVisibility: true, // 始终显示滚动条
            thickness: 6.0, // 滚动条宽度
            radius: Radius.circular(10), // 圆角滚动条
            child: child,
          ),
        ),
      ),
    );
  }
}


class SukietListViewScrollBehaviorPlus extends ScrollBehavior {
  @override
  Widget buildScrollbar(BuildContext context, Widget child, ScrollableDetails details) {
    return _AnimatedScrollbarWrapper(
      controller: details.controller!,
      child: child,
    );
  }
}

class _AnimatedScrollbarWrapper extends StatefulWidget {
  final ScrollController controller;
  final Widget child;

  const _AnimatedScrollbarWrapper({
    super.key,
    required this.controller,
    required this.child,
  });

  @override
  State<_AnimatedScrollbarWrapper> createState() => _AnimatedScrollbarWrapperState();
}

class _AnimatedScrollbarWrapperState extends State<_AnimatedScrollbarWrapper>
    with SingleTickerProviderStateMixin {
  late AnimationController _animationController;
  late Animation<Color?> _colorAnimation;

  // 当前是否处于 hover / drag 状态
  bool _isHoveredOrDragged = false;

  @override
  void initState() {
    super.initState();
    _animationController = AnimationController(
      duration: const Duration(milliseconds: 250),
      vsync: this,
    );

    _colorAnimation = ColorTween(
      begin: Colors.white60,
      end: Colors.white,
    ).animate(CurvedAnimation(
      parent: _animationController,
      curve: Curves.easeInOut,
    ));
  }

  void _updateHoverState(bool hoverOrDrag) {
    if (_isHoveredOrDragged != hoverOrDrag) {
      _isHoveredOrDragged = hoverOrDrag;
      if (_isHoveredOrDragged) {
        _animationController.forward();
      } else {
        _animationController.reverse();
      }
    }
  }

  @override
  void dispose() {
    _animationController.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return MouseRegion(
      onEnter: (_) => _updateHoverState(true),
      onExit: (_) => _updateHoverState(false),
      child: NotificationListener<ScrollNotification>(
        onNotification: (notification) {
          // 判断是否正在拖动
          final isDragging = notification is ScrollStartNotification ||
              notification is UserScrollNotification &&
                  notification.direction != ScrollDirection.idle;

          _updateHoverState(isDragging);
          return false;
        },
        child: AnimatedBuilder(
          animation: _colorAnimation,
          builder: (context, _) {
            return ScrollbarTheme(
              data: ScrollbarThemeData(
                thumbColor: WidgetStateProperty.all(_colorAnimation.value),
              ),
              child: Scrollbar(
                controller: widget.controller,
                thumbVisibility: true,
                thickness: 6.0,
                radius: const Radius.circular(10),
                child: widget.child,
              ),
            );
          },
        ),
      ),
    );
  }
}
